const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
const config = require('../config')
const db = require('../db/index')
module.exports = (req, res) => {
    const sql = 'SELECT * from user WHERE username=?'
    db(sql, req.body.username, result => {
        console.log("result", result)
        if (result.length !== 1) {
            return res.send({
                status: 1,
                msg: '此用户并不纯在'
            })
        }
        const psd =  bcrypt.compareSync(req.body.password, result[0].password)
        if(!psd) {
            return res.send({
                status: 1,
                msg: '密码错误'
            })
        }
        const token = jwt.sign({ username: req.body.username}, config.jwtKey, { expiresIn: '1h' }) 
        res.send({
            status: 0,
            msg: '登录成功xx',
            token
        })
    })

}